﻿using System;
using System.Net;
using System.Threading.RateLimiting;
using Common;
using Microsoft.AspNetCore.Http;
using static Org.BouncyCastle.Math.EC.ECCurve;

namespace Extensions.Services.RateLimit.Policys;
/// <summary>
/// 令牌桶
/// </summary>
public class TokenBucketLimiterPolicy:BaseLimiterPolicy
{
    protected override RateLimitPartition<IPAddress> RateLimit(IPAddress ipAddress)
    {
        return RateLimitPartition.GetTokenBucketLimiter(ipAddress, _ => new TokenBucketRateLimiterOptions()
        {
            // 最大令牌数
            TokenLimit = base.Config.TokenLimit,
            //队列处理顺序  
            QueueProcessingOrder = QueueProcessingOrder.OldestFirst,
            // 等待队列的长度
            QueueLimit = base.Config.QueueLimit,
            //指定补充令牌的最短期限。
            ReplenishmentPeriod = TimeSpan.FromSeconds(base.Config.ReplenishmentPeriod),
            //指定每次补充的最大令牌数。
            TokensPerPeriod = base.Config.TokensPerPeriod
        });
    }
}